package com.happyass.controller;

import com.happyass.pojo.User;
import com.happyass.pojo.page.PageInfo;
import com.happyass.service.UserService;
import com.happyass.service.excel.UserExcelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.format.datetime.DateFormatter;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
      //注入
    @Autowired
    private UserService userService;

    //excel
    @Autowired(required = false)
    UserExcelService excelService;
    //注入上传文件的绝对物理目录路径
    @Value("${file.address}")
    private String fileAddres;

    //注入访问上传文件的静态资源路径
    @Value("${file.staticPath}")
    private String fileStaicPath;


    @InitBinder
    public void initDateFormate(WebDataBinder dataBinder) {
        dataBinder.addCustomFormatter(new DateFormatter("yyyy-MM-dd"));
    }

    //访问用户信息页面
    //跳转list页面
    @RequestMapping("/list")
    public String list(Model model) {
        List<User> userInfoList = userService.selectAll();
        model.addAttribute("list", userInfoList);
        return "Table/userTable";
    }

    //访问增加用户
    @RequestMapping("/add")
    public String Add() {
        return "user/userAdd";
    }
    @ResponseBody
    @RequestMapping("/submitAdd")
//    public ResultEntity<User> insertCategory(@RequestBody User user) {
//        return userService.insertUser(user);
//    }
    public HashMap<String, Object> insertUser(User user,String city,String province,String county ) throws ParseException {
        String userArr = province + city + county;
       return userService.insertUser(user,userArr);
 }
    //修改用户
    @RequestMapping("/edit")
    public String edit(@RequestParam("userId") Integer userId, Model model){
        User userInfo = userService.selectById(userId);
        model.addAttribute("user",userInfo);
        return "user/userEdit";
    }
    //保存
    @RequestMapping("/save/{path}")
    @ResponseBody
    public boolean save(User user, @PathVariable("path") String path){
        //修改
        return userService.edit(user);
    }

    //删除用户
    @RequestMapping("/del")
    @ResponseBody
    public boolean del( @RequestParam("userId") Integer userId){
        return userService.del(userId);
    }

    //查看用户
    @RequestMapping("/find")
    @ResponseBody
    public String find(@RequestParam("userId") Integer userId, Model model){
        User userInfo = userService.selectById(userId);
        model.addAttribute("user",userInfo);
        return "user/userEdit";
    }

    //修改显示状态
    @RequestMapping("/show")
    @ResponseBody
    public Boolean show(Integer userId,Integer userStatus) {
        return userService.editStatus(userId,userStatus);}

    //批量删除
    @RequestMapping("/delAll")
    @ResponseBody
    public boolean delAll(String userIds) {

        return userService.delAll(userIds);
    }

    //分页
    @RequestMapping("/allPageList")
    public String allPageList(Model model, PageInfo pageInfo){
        Page<User> userPage  = userService.selectAllUser(pageInfo.getPage(),pageInfo.getRow());
        //当前页
        model.addAttribute("curPage",userPage.getNumber()+1);
        //下一页
        if ((userPage.getNumber()+2)> userPage.getTotalPages()){
            model.addAttribute("nextPage",(userPage.getNumber()+1));}
        else {
            model.addAttribute("nextPage",(userPage.getNumber())+2);
        }
        //上一页
        if (userPage.getNumber()<=0) {
            model.addAttribute("prePage", userPage.getNumber()+1);
        }else{
            model.addAttribute("prePage", userPage.getNumber());
        }
        //总页数
        model.addAttribute("totalPages",userPage.getTotalPages());
        //总条数
        model.addAttribute("totalElements",userPage.getTotalElements());
        // 当期页集合
        model.addAttribute("list",userPage.getContent());
        return "/Table/userTable";
    }
//____________________________________________________________
//excel导出
@RequestMapping("/excel")
public void excel(HttpServletResponse response) throws IOException {
    excelService.excel(response);}
}
